﻿using BaoVeBienCuong.Playgame.maps;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;

namespace BaoVeBienCuong.Playgame.enemy
{
    public class Enemy
    {
        #region
        int count = 0;// biến phụ dùng làm ảnh động
        bool isEnable; // còn sống hay ko ?
        int Loai; // loại địch
        float TocDo; // tốc độ chạy
        string Huong; // hướng đi
        float Mau; // máu khởi điểm
        Vector2 position; // vị trí trên màn hình
        Vector2 size; // kích thước
        Vector2 sizeImages; // kích thước ảnh amination 
        float curmau; // máu hiện thời
        Texture2D mauImage; // hình ảnh máu
        Texture2D khungmau; // hình ảnh khung máu
        Texture2D image;// ảnh load của địch
        Vector2 index;// chỉ số đứng của địch trên ma trận
        float Giap; // chỉ số giáp
        float scale; // chỉ số phóng hình
        Vector2 DoLechAnh;//độ lệch để căn chỉnh hình ảnh hiệu quả
        Vector2 DoLechMau;// độ lệch vẽ máu cho chuẩn
        int value;
        string hieuung = "";
        int counthieuung = 0;// biến phụ để gây chảy máu
        Map map;
        bool isNe;
        Texture2D neImage;
        int countNe;
        Random rand;
        Vector2 indexRe;
        #endregion
        public Enemy(int loai, float tocdo, float mau, string huong, float giap, Vector2 index)
        {
            map = GamePage.map;
            indexRe = new Vector2(0, 0);
            countNe = 0;
            rand = new Random();
            isNe = false;
            isEnable = true;
            Loai = loai;
            TocDo = tocdo;
            Mau = mau;
            curmau = mau;
            Huong = huong;
            Giap = giap;
            this.index = index;
            DoLechAnh = new Vector2(0, 0);
            DoLechMau = new Vector2(0, 0);
            size = new Vector2(GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y, GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X);
            position = new Vector2(index.Y * size.X, index.X * size.Y);
        }

        // nơi đặt get/set các thuộc tính
        #region
        public Vector2 getPosition()
        {
            return position;
        }
        public Vector2 getSize()
        {
            return size;
        }
        public Vector2 getIndex()
        {
            return index;
        }
        public bool getEnable()
        {
            return isEnable;
        }
        public Vector2 getTam()
        {
            return position;
        }
        public int getLoai()
        {
            return Loai;
        }
        public void setTruMau(float dame, string hieuung = "", bool isNeAvailable = true)
        {
            if (isNeAvailable == true)
            {
                if (!isNe)
                {
                    if (rand.Next(1000) / 10 >= 90 - GamePage.lever / 2)
                    {
                        isNe = true;
                        countNe = 0;
                    }
                    else
                    {
                        if (Loai == 5) curmau -= dame;
                        else
                        {
                            this.hieuung = hieuung;
                            if (hieuung.Equals("chay mau") || hieuung.Equals("lam cham") || hieuung.Equals("lam do")) counthieuung = 0;
                            else if (hieuung.Equals("dot tu")) curmau = 0;
                            else if (hieuung.Equals("chi mang")) curmau -= dame * 1.5f;
                            else if (hieuung.Equals("xuyen giap")) curmau -= dame * 1.2f;
                            else curmau -= dame;
                        }
                        if (curmau <= 0) isEnable = false;
                    }
                }
            }
            else
            {
                if (Loai == 5) curmau -= dame;
                else
                {
                    this.hieuung = hieuung;
                    if (hieuung.Equals("chay mau") || hieuung.Equals("lam cham") || hieuung.Equals("lam do")) counthieuung = 0;
                    else if (hieuung.Equals("dot tu")) curmau = 0;
                    else if (hieuung.Equals("chi mang")) curmau -= dame * 1.5f;
                    else if (hieuung.Equals("xuyen giap")) curmau -= dame * 1.2f;
                    else curmau -= dame;
                }
                if (curmau <= 0) isEnable = false;
            }
        }
        public float getCurMau()
        {
            return curmau;
        }
        public string getHuong()
        {
            return Huong;
        }
        public float getMau()
        {
            return Mau;
        }
        public int getValue()
        {
            return value;
        }
        #endregion

        // nơi xử lý các giá trị DoLechAnh va DoLechMau
        #region
        public void setSizeimages()
        {
            switch (Loai)
            {
                case 0:
                    {
                        #region
                        value = 20;
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(582, 163);
                            DoLechAnh = new Vector2(20, -50) * scale;
                            DoLechMau = new Vector2(15, -70) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(588, 163);
                            DoLechAnh = new Vector2(10, -65) * scale;
                            DoLechMau = new Vector2(20, -90) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(882, 138);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(5, -55) * scale;
                                DoLechMau = new Vector2(15, -70) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                {
                                    DoLechAnh = new Vector2(-20, -55) * scale;
                                }
                        }
                        break;
                        #endregion
                    }
                case 1:
                    {
                        value = 25;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(1140, 176);
                            DoLechAnh = new Vector2(-30, -90) * scale;
                            DoLechMau = new Vector2(10, -90) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(1272, 153);
                            DoLechAnh = new Vector2(-40, -70) * scale;
                            DoLechMau = new Vector2(15, -90) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(900, 164);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(-30, -70) * scale;
                                DoLechMau = new Vector2(10, -90) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                {
                                    DoLechAnh = new Vector2(10, -70) * scale;
                                }
                        }
                        break;
                        #endregion
                    }
                case 2:
                    {
                        value = 15;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(888, 148);
                            DoLechAnh = new Vector2(20, -70) * scale;
                            DoLechMau = new Vector2(10, -90) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(1086, 152);
                            DoLechAnh = new Vector2(-70, -70) * scale;
                            DoLechMau = new Vector2(15, -90) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(762, 174);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(10, -90) * scale;
                                DoLechMau = new Vector2(0, -90) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                    DoLechAnh = new Vector2(-10, -90) * scale;
                        }

                        break;
                        #endregion
                    }
                case 3:
                    {
                        value = 0;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(632, 242);
                            DoLechAnh = new Vector2(-20, -70) * scale;
                            DoLechMau = new Vector2(10, -90) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(632, 242);
                            DoLechAnh = new Vector2(-15, -70) * scale;
                            DoLechMau = new Vector2(15, -90) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(1080, 144);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(-50, -30) * scale;
                                DoLechMau = new Vector2(25, -70) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                    DoLechAnh = new Vector2(-80, -30) * scale;
                        }
                        break;
                        #endregion
                    }
                case 4:
                    {
                        value = 15;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(780, 160);
                            DoLechAnh = new Vector2(0, -75) * scale;
                            DoLechMau = new Vector2(10, -90) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(564, 187);
                            DoLechAnh = new Vector2(15, -80) * scale;
                            DoLechMau = new Vector2(15, -110) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(750, 182);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(25, -80) * scale;
                                DoLechMau = new Vector2(10, -100) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                    DoLechAnh = new Vector2(-15, -80) * scale;
                        }
                        break;
                        #endregion

                    }
                case 5:
                    {
                        value = 15;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(828, 152);
                            DoLechAnh = new Vector2(-5, -70) * scale;
                            DoLechMau = new Vector2(10, -80) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(840, 160);
                            DoLechAnh = new Vector2(-5, -80) * scale;
                            DoLechMau = new Vector2(15, -100) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(654, 158);
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(15, -75) * scale;
                                DoLechMau = new Vector2(15, -90) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                    DoLechAnh = new Vector2(5, -80) * scale;
                        }
                        break;
                        #endregion

                    }
                default:
                    {
                        value = 300;
                        #region
                        if (Huong.Equals("Top"))
                        {
                            sizeImages = new Vector2(1512, 230) / 2;
                            DoLechAnh = new Vector2(-70, -135) * scale;
                            DoLechMau = new Vector2(10, -145) * scale;
                        }
                        else if (Huong.Equals("Bottom"))
                        {
                            sizeImages = new Vector2(1680, 244) / 2;
                            DoLechAnh = new Vector2(-60, -135) * scale;
                            DoLechMau = new Vector2(15, -155) * scale;
                        }
                        else
                        {
                            sizeImages = new Vector2(1098, 304) / 2;
                            if (Huong.Equals("Right"))
                            {
                                DoLechAnh = new Vector2(-65, -140) * scale;
                                DoLechMau = new Vector2(10, -145) * scale;
                            }
                            else
                                if (Huong.Equals("Left"))
                                    DoLechAnh = new Vector2(15, -140) * scale;
                        }
                        break;
                        #endregion

                    }
            }
        }
        #endregion

        public void Dichuyen()
        {
            int giamtocdo = 0;
            //hiệu ứng chảy máu theo thời gian
            #region
            if (!hieuung.Equals("") && counthieuung < 60)
            {
                if (hieuung.Equals("chay mau"))
                {
                    giamtocdo = 0;
                    curmau -= 2 + GamePage.lever * 2;
                    if (curmau <= 0) isEnable = false;
                }
                else if (hieuung.Equals("lam cham"))
                {
                    giamtocdo = 50;
                }
                else if (hieuung.Equals("lam do"))
                {
                    giamtocdo = 100;
                }
                counthieuung++;
            }
            else
            {
                hieuung = "";
            }
            #endregion

            //tính vị trí hiện thời
            #region
            index.Y = (int)(position.X) / ((int)GamePage.sizeImageBackGround.X / (int)GamePage.sizeMap.Y);
            index.X = (int)(position.Y) / ((int)GamePage.sizeImageBackGround.Y / (int)GamePage.sizeMap.X);
            if (index.X < 0) index.X = 0;
            if (index.Y < 0) index.Y = 0;
            if (index.X >= GamePage.sizeMap.X) index.X = GamePage.sizeMap.X - 1;
            if (index.Y >= GamePage.sizeMap.Y) index.Y = GamePage.sizeMap.Y - 1;
            #endregion

            //xét việc chuyển hướng đi
            #region
            {
                switch (Huong)
                {
                    case "Top":
                        #region
                        if (Math.Abs(position.Y - (index.X) * (GamePage.sizeImageBackGround.Y / GamePage.sizeMap.X)) <= 10)
                        {
                            if (!map.XacDinhRe(GamePage.lever, index, Huong).Equals(Huong) && index != indexRe)
                            {
                                Huong = map.XacDinhRe(GamePage.lever, index, Huong);
                                indexRe = index;
                            }
                            else
                                if ((index.X == 0) || GamePage.curmap[(int)index.X - 1, (int)index.Y] != 1)
                                {
                                    if (((int)index.Y < GamePage.sizeMap.Y - 1) && (GamePage.curmap[(int)index.X, (int)index.Y + 1] == 1))
                                    {
                                        Huong = "Right";
                                    }
                                    else if (((int)index.Y > 0) && (GamePage.curmap[(int)index.X, (int)index.Y - 1] == 1))
                                    {
                                        Huong = "Left";
                                    }
                                    else
                                    {
                                        isEnable = false;
                                    }
                                }
                        }
                        #endregion
                        break;
                    case "Left":
                        #region
                        if (Math.Abs(position.X - (index.Y) * (GamePage.sizeImageBackGround.X / GamePage.sizeMap.Y)) <= 10)
                        {
                            if (!map.XacDinhRe(GamePage.lever, index, Huong).Equals(Huong) && index != indexRe)
                            {
                                Huong = map.XacDinhRe(GamePage.lever, index, Huong);
                                indexRe = index;
                            }
                            else
                                if ((index.Y == 0) || GamePage.curmap[(int)index.X, (int)index.Y - 1] != 1)
                                {
                                    if (((int)index.X > 0) && (GamePage.curmap[(int)index.X - 1, (int)index.Y] == 1))
                                    {
                                        Huong = "Top";
                                    }
                                    else if (((int)index.X < GamePage.sizeMap.X - 1) && (GamePage.curmap[(int)index.X + 1, (int)index.Y] == 1))
                                    {
                                        Huong = "Bottom";
                                    }
                                    else
                                    {
                                        isEnable = false;
                                    }
                                }
                        }
                        #endregion
                        break;
                    case "Right":
                        #region
                        if (!map.XacDinhRe(GamePage.lever, index, Huong).Equals(Huong) && index != indexRe)
                        {
                            Huong = map.XacDinhRe(GamePage.lever, index, Huong);
                            indexRe = index;
                        }
                        else
                            if ((index.Y == GamePage.sizeMap.Y - 1) || GamePage.curmap[(int)index.X, (int)index.Y + 1] != 1)
                            {
                                if (((int)index.X > 0) && (GamePage.curmap[(int)index.X - 1, (int)index.Y] == 1))
                                {
                                    Huong = "Top";
                                }
                                else if (((int)index.X < GamePage.sizeMap.X - 1) && (GamePage.curmap[(int)index.X + 1, (int)index.Y] == 1))
                                {
                                    Huong = "Bottom";
                                }
                                else
                                {
                                    isEnable = false;
                                }
                            }
                        #endregion
                        break;
                    case "Bottom":
                        #region
                        if (!map.XacDinhRe(GamePage.lever, index, Huong).Equals(Huong) && index != indexRe)
                        {
                            Huong = map.XacDinhRe(GamePage.lever, index, Huong);
                            indexRe = index;
                        }
                        else
                            if ((index.X == GamePage.sizeMap.X - 1) || GamePage.curmap[(int)index.X + 1, (int)index.Y] != 1)
                            {
                                if (((int)index.Y < GamePage.sizeMap.Y - 1) && (GamePage.curmap[(int)index.X, (int)index.Y + 1] == 1))
                                {
                                    Huong = "Right";
                                }
                                else if (((int)index.Y > 0) && (GamePage.curmap[(int)index.X, (int)index.Y - 1] == 1))
                                {
                                    Huong = "Left";
                                }
                                else
                                {
                                    isEnable = false;
                                }
                            }
                        #endregion
                        break;
                    default: break;
                }
            }
            #endregion

            //dịch chuyển
            #region
            if (Huong.Equals("Bottom"))
            {
                position.Y += (TocDo - TocDo * giamtocdo / 100) / 2;
            }
            else if (Huong.Equals("Left"))
            {
                position.X -= (TocDo - TocDo * giamtocdo / 100) / 2;
            }
            else if (Huong.Equals("Top"))
            {
                position.Y -= (TocDo - TocDo * giamtocdo / 100) / 2;
            }
            else
            {
                position.X += (TocDo - TocDo * giamtocdo / 100) / 2;
            }
            #endregion

            //nếu ra ngoài map : đến đích
            /*if (position.Y >= GamePage.sizeImageBackGround.Y || position.Y <= 0 || position.X >= GamePage.sizeImageBackGround.X || position.X <= 0)
            {
                isEnable = false;
            }*/
        }
        public void Load(ContentManager cont)
        {
            mauImage = cont.Load<Texture2D>("images/enemys/HP");
            khungmau = cont.Load<Texture2D>("images/enemys/HP_bar");
            if (isNe)
            {
                neImage = cont.Load<Texture2D>("images/enemys/miss");
            }
            if (Huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("images/enemys/enemy_" + (Loai * 3 + 1));
            }
            else if (Huong.Equals("Top"))
            {
                image = cont.Load<Texture2D>("images/enemys/enemy_" + (Loai * 3 + 2));
            }
            else
            {
                image = cont.Load<Texture2D>("images/enemys/enemy_" + Loai * 3);
            }
            setSizeimages();
            size = new Vector2(sizeImages.X / 6 * GamePage.scale, sizeImages.Y * GamePage.scale);
        }
        public void Draw(SpriteBatch sprite)
        {
            Color curColor = new Color();
            if (hieuung.Equals("lam cham") || hieuung.Equals("lam do")) curColor = Color.Blue;
            else if (hieuung.Equals("dot tu") || hieuung.Equals("chi mang") || hieuung.Equals("xuyen giap")) curColor = Color.Red;
            else if (hieuung.Equals("chay mau")) curColor = Color.Green;
            else curColor = Color.White;
            scale = GamePage.scale;
            if (isEnable)
            {
                sprite.Draw(mauImage, position * scale + DoLechMau + GamePage.DoLech, new Rectangle(0, 0, (int)(curmau * 99 / Mau), 8),
                                            Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                sprite.Draw(khungmau, position * scale + DoLechMau + GamePage.DoLech, new Rectangle(0, 0, 101, 8),
                                            Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                if (Huong.Equals("Right"))
                {
                    sprite.Draw(image, position * scale + DoLechAnh + GamePage.DoLech, new Rectangle((int)sizeImages.X / (Loai == 3 ? 4 : 6) * (count / 6), 0, (int)sizeImages.X / (Loai == 3 ? 4 : 6), (int)sizeImages.Y),
                        curColor, 0, Vector2.Zero, Loai == 6 ? scale * 2 : scale, SpriteEffects.None, 0);
                }
                else
                {
                    sprite.Draw(image, position * scale + DoLechAnh + GamePage.DoLech, new Rectangle((int)sizeImages.X / (Loai == 3 ? 4 : 6) * (count / 6), 0, (int)sizeImages.X / (Loai == 3 ? 4 : 6), (int)sizeImages.Y),
                                            curColor, 0, Vector2.Zero, Loai == 6 ? scale * 2 : scale, SpriteEffects.FlipHorizontally, 0);
                }
                if (isNe)
                {
                    sprite.Draw(neImage, position * scale + DoLechMau + GamePage.DoLech, new Rectangle(0, 0, 92, 76),
                                            Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
                    if (!GamePage.isPause) countNe++;
                    if (countNe >= 10 + GamePage.lever / 2) isNe = false;
                }
                if (!GamePage.isPause) count++;
                if (count == (Loai == 3 ? 24 : 36)) count = 0;
            }
        }
        public bool isGiua()
        {
            Vector2 tam = new Vector2(GamePage.sizeImageBackGround.X / (int)GamePage.sizeMap.Y * index.Y + 400 / (int)GamePage.sizeMap.Y, GamePage.sizeImageBackGround.Y / (int)GamePage.sizeMap.X * index.X + 240 / (int)GamePage.sizeMap.X);
            if (position.X + size.X / 2 - TocDo <= tam.X && (position.X + size.X / 2 - TocDo) >= tam.X)
            {
                if (position.Y + size.Y - TocDo - 4 <= tam.Y && position.Y + size.Y + TocDo - 4 >= tam.Y)
                {
                    return true;
                }
            }
            return false;
        }
        public bool isVaCham(Vector2 posi)
        {
            if (posi.X >= (position.X + 5) && posi.X <= (position.X + size.X - 5) && posi.Y >= (position.Y + 5) && posi.Y <= (position.Y + size.Y - 5))
            {
                return true;
            }
            else return false;
        }
    }
}
